/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-10-21     xu           the first version
 */
#include "key_thread_init.h"
#include "drv_gpio.h"
#include "multi_button.h"

static struct rt_messagequeue ui_key_mq;
static rt_uint8_t ui_key_msgpool[2048];

static struct button btn;
static rt_base_t BUTTON_PIN = GET_PIN(C, 13);

static uint8_t button_read_pin(void)
{
    return rt_pin_read(BUTTON_PIN);
}

void button_callback(void* btn)
{
    uint32_t btn_event_val;

    btn_event_val = get_button_event((struct button*)btn);

    ui_key_msg_t key_msg = {0};
    key_msg.key = KEY_K1;
    switch (btn_event_val) {
        case PRESS_DOWN:
            key_msg.state = STATE_PRESS_DOWN;
            rt_kprintf("STATE_PRESS_DOWN\n");
            break;
        case PRESS_UP:
            key_msg.state = STATE_PRESS_UP;
            rt_kprintf("STATE_PRESS_UP\n");
            break;
        case PRESS_REPEAT:
            key_msg.state = STATE_PRESS_REPEAT;
            rt_kprintf("STATE_PRESS_REPEAT\n");
            break;
        case SINGLE_CLICK:
            key_msg.state = STATE_SINGLE_CLICK;
            rt_kprintf("STATE_SINGLE_CLICK\n");
            break;
        case DOUBLE_CLICK:
            key_msg.state = STATE_DOUBLE_CLICK;
            rt_kprintf("STATE_DOUBLE_CLICK\n");
            break;
        case LONG_PRESS_START:
            key_msg.state = STATE_LONGPRESS_START;
            rt_kprintf("STATE_LONGPRESS_START\n");
            break;
        case LONG_PRESS_HOLD:
            key_msg.state = STATE_LONGPRESS_HOLD;
            rt_kprintf("STATE_LONGPRESS_HOLD\n");
            break;
    }
    rt_mq_send(&ui_key_mq, &key_msg, sizeof(key_msg));
}

void btn_thread_entry(void* p)
{
    while (1) {
        rt_thread_delay(10);
        button_ticks();
    }
}

int multi_button_init(void)
{
    rt_thread_t thread = RT_NULL;

    rt_pin_mode(BUTTON_PIN, PIN_MODE_INPUT);

    button_init(&btn, button_read_pin, PIN_HIGH);
    button_attach(&btn, PRESS_DOWN, button_callback);
    button_attach(&btn, PRESS_UP, button_callback);
    button_attach(&btn, PRESS_REPEAT, button_callback);
    button_attach(&btn, SINGLE_CLICK, button_callback);
    button_attach(&btn, DOUBLE_CLICK, button_callback);
    button_attach(&btn, LONG_PRESS_START, button_callback);
    button_attach(&btn, LONG_PRESS_HOLD, button_callback);

    button_start(&btn);

    /* Create background ticks thread */
    thread = rt_thread_create("btn", btn_thread_entry, RT_NULL, 1024, 10, 10);
    if (thread == RT_NULL) {
        return RT_ERROR;
    }
    RT_ASSERT(rt_thread_startup(thread) == RT_EOK);

    return RT_EOK;
}
INIT_APP_EXPORT(multi_button_init);

rt_mq_t ui_key_mq_get()
{
    return &ui_key_mq;
}

static int ui_key_mq_init()
{
    rt_err_t err = rt_mq_init(&ui_key_mq, "key_mq", ui_key_msgpool, // message pool
                              sizeof(ui_key_msg_t), sizeof(ui_key_msgpool), RT_IPC_FLAG_FIFO);
    if (err != RT_EOK) {
        rt_kprintf("create key_mq failed!\n");
        return RT_NULL;
    }
    rt_kprintf("create key_mq success!\n");
}
INIT_ENV_EXPORT(ui_key_mq_init);

